/*******************************************************************************
   reference ajax-functions
*/

function initTableDND()
{
   $('#refTbl').tableDnD();

   $('#refTbl').tableDnD({ onDrop: function(table, row)
   {
      var q = 'where='+ref_rfctype()+'&what=order&'+$.tableDnD.serialize();

      $.post(basedomain+'/editrfc', q, function(data){ update_refc(data); });
   }
   });
}

$(document).ready(function() {
   initTableDND();
});


function update_refc(data)
{
   $('#dvrefc').html(data);
   
   initTableDND();
}

function ref_addnewform()
{
   $('#newRefItem').fadeIn('fast');
}

function ref_CancelAddNew(msg)
{
   $('#newRefItem').fadeOut('fast');
}

function ref_AddNew(name)
{
   if(name.length>2)
   {
      $.post(basedomain+'/editrfc', {what:'addnew', name:name, where: ref_rfctype()}, function(data){ update_refc(data); } );
   }
   
   ref_CancelAddNew(0);
}


function ref_edit(id)
{
   objItemName = document.getElementById('rfcItemName'+id);
   objItemInpt = document.getElementById('rfcEditField'+id);
   
   objBtEStart = document.getElementById('rfcBttEditStart'+id);
   objBtDelete = document.getElementById('rfcBttDelete'+id);
   objBtESend  = document.getElementById('rfcBttEditSend'+id);
   objBtECancel = document.getElementById('rfcBttEditCancel'+id);
   
   objItemName.style.display = 'none';
   objItemInpt.style.display = 'inline';
   
   objBtEStart.style.display = 'none';
   objBtDelete.style.display = 'none';
   
   objBtESend.style.display = 'inline';
   objBtECancel.style.display = 'inline';

}

function ref_edit_cancel(id)
{
   objItemName = document.getElementById('rfcItemName'+id);
   objItemInpt = document.getElementById('rfcEditField'+id);

   objBtEStart = document.getElementById('rfcBttEditStart'+id);
   objBtDelete = document.getElementById('rfcBttDelete'+id);
   objBtESend  = document.getElementById('rfcBttEditSend'+id);
   objBtECancel = document.getElementById('rfcBttEditCancel'+id);

   objItemName.style.display = 'inline';
   objItemInpt.style.display = 'none';

   objBtEStart.style.display = 'inline';
   objBtDelete.style.display = 'inline';

   objBtESend.style.display = 'none';
   objBtECancel.style.display = 'none';
}


function ref_edit_send(id, num)
{
   iname = '#rfcItemName'+id;
   iinpt = '#rfcEditInput'+id;
   
   name = $(iinpt).val();
   
   if(id>0 && $(iname).html() != name && name.length>2)
   {
      $.post(basedomain+'/editrfc', {what:'edit', id: id, num: num, name: name, where: ref_rfctype()}, function(data){ update_refc(data); } );
   }
   else if($(iname).html() == name)
   {
      ref_edit_cancel(id);
   }
   else alert('oops!');
}

function ref_del(id, msg)
{
   if(id>0 && confirm(msg))
   {
      $.post(basedomain+'/editrfc', {what:'delete', id: id, where: ref_rfctype()}, function(data){ update_refc(data); } );
   }
}




